home *** CD-ROM | disk | FTP | other *** search
/ Libris Britannia 4 / science library(b).zip / science library(b) / ELECTRON / PCB_DESI / 1540.ZIP / PCBCA110.ZIP / PCBRAT.C < prev    next >
C/C++ Source or Header  |  1992-08-27  |  11KB  |  371 lines

  1. /*
  2. ** printed circuit board rat nester, Copyright (C) Randy Nevin 1989, 1990.
  3. **
  4. ** you may give this software to anyone, make as many copies as you like, and
  5. ** post it on public computer bulletin boards and file servers. you may not
  6. ** sell it or charge any fee for distribution (except for media and postage),
  7. ** remove this comment or the copyright notice from the code, or claim that
  8. ** you wrote this code or anything derived from it. you may modify the code as
  9. ** much as you want (please document clearly with comments, and maintain the
  10. ** coding style), but programs which are derived from this one are subject to
  11. ** the conditions stated here. i am providing this code so that people can
  12. ** learn from it, so if you distribute it, please include source code, not
  13. ** just executables. contact me to report bugs or suggest enhancements; i do
  14. ** not guarantee support, but i will make an effort to help you, and i want to
  15. ** act as a central clearing house for future versions. you should contact me
  16. ** before undertaking a significant development effort, to avoid reinventing
  17. ** the wheel. if you come up with an enhancement you consider particularly
  18. ** useful, i would appreciate being informed so that it can be incorporated in
  19. ** future versions. my address is: Randy Nevin, 24135 SE 16th PL, Issaquah,
  20. ** WA 98027, USA. this code is available directly from the author; just send a
  21. ** 360k floppy and a self-addressed floppy mailer with sufficient postage.
  22. **
  23. ** HISTORY
  24. ** (name        date        description)
  25. ** ----------------------------------------------------
  26. ** randy nevin        5/7/89        initial version
  27. ** randy nevin        5/7/89        released version 1.00
  28. ** randy nevin        12/27/89    fixed graphics dot-drawing bugs
  29. ** randy nevin        12/27/89    released version 1.10
  30. ** randy nevin        1/18/92        1.20, change 18x18->15x15
  31. */
  32.  
  33. #include <stdio.h>
  34. #include <stdlib.h>
  35.  
  36. #ifndef VMS
  37. #include <io.h>
  38. #include <dos.h>
  39. #endif
  40.  
  41. #include <string.h>
  42. #include "cell.h"
  43.  
  44. /* WARNING: the code below assumes 640x350 16-color ega */
  45.  
  46. /* 0=black    1=blue        2=green        3=light blue        */
  47. /* 4=red    5=purple    6=brown        7=grey            */
  48. /* 8=black    9=bright blue    A=bright green    B=bright light blue    */
  49. /* C=scarlet    D=purple    E=yellow    F=white            */
  50.  
  51. /*
  52. ** the colors below work fine for me, but may not for your particular ega and
  53. ** monitor. if things don't look right, you may want to change these
  54. ** constants.
  55. */
  56.  
  57. /* colors of objects */
  58. #define H    0xC    /* hole color; scarlet        */
  59. #define T    0x9    /* trace color; bright blue    */
  60. #define E    0xE    /* edge color; yellow        */
  61.  
  62. /* screen limits */
  63. #define MINHORZ    0    /* left-most pixel    */
  64. #define MAXHORZ    639    /* right-most pixel    */
  65. #define MINVERT    0    /* top-most pixel    */
  66. #define MAXVERT    349    /* bottom-most pixel    */
  67.  
  68. static int mode; /* for saving original screen mode */
  69.  
  70. #define MAXZOOM    3    /* maximum zoom number; minimum is 0 */
  71.  
  72. #define ZOOM0    3    /* 3x3 pixels per cell        */
  73. #define ZOOM1    6    /* 6x6 pixels per cell        */
  74. #define ZOOM2    10    /* 10x10 pixels per cell    */
  75. #define ZOOM3    15    /* 15x15 pixels per cell    */
  76.  
  77. static int zoom = 1; /* 0=3x3, 1=6x6, 2=10x10, 3=15x15 */
  78. static int zoomsize[MAXZOOM+1] = { ZOOM0, ZOOM1, ZOOM2, ZOOM3 };
  79.  
  80. /* current lower-left position */
  81. static int Xrow = 0;
  82. static int Xcol = 0;
  83.  
  84. extern int Nrows, Ncols; /* board dimensions */
  85.  
  86. int JustBoard = 1; /* only need the board data structure */
  87. int SortConnects = 0; /* default is to not sort non-PRIORITY CONNECTs */
  88.  
  89. extern int Initialize( char *, int );
  90. extern void ReInitWork( void );
  91. extern void GetWork( int *, int *, char far * far *, int *, int *,
  92.     char far * far * );
  93. extern long GetCell( int, int, int );
  94. extern int GetMode( void );
  95. extern void SetMode( int );
  96. extern void Line( int, int, int, int, int );
  97. extern void Dot( int, int, int );
  98.  
  99. void main( int, char *[] );
  100. static void doedges( void );
  101. static void doboard( void );
  102. static void plot0( int, int, char [ZOOM0][ZOOM0], int );
  103. static void plot1( int, int, char [ZOOM1][ZOOM1], int );
  104. static void plot2( int, int, char [ZOOM2][ZOOM2], int );
  105. static void plot3( int, int, char [ZOOM3][ZOOM3], int );
  106.  
  107. void main ( argc, argv ) /* input board, display rat nest on the screen */
  108.     int argc;
  109.     char *argv[];
  110.     {
  111.     char *self, *p;
  112.     union REGS regs;
  113.     int i;
  114.  
  115.     printf( "Copyright (C) Randy Nevin, 1989, 1990. Version 1.20\n" );
  116.     printf( "See source code for rights granted.\n\n" );
  117.     self = argv[0];
  118.     /* get rid of initial part of path */
  119.     if ((p = strrchr( self, '\\' )) || (p = strrchr( self, ':' )))
  120.         self = ++p;
  121.     /* get rid of extension */
  122.     if ((p = strrchr( self, '.' )) && !stricmp( p, ".EXE" ))
  123.         *p = 0;
  124.     if (argc != 2) { /* need infile */
  125.         fprintf( stderr, "usage: %s infile\n", self );
  126.         exit( -1 );
  127.         }
  128.     i = Initialize( argv[1], 0 ); /* don't echo memory used */
  129.     /* tell user what commands are available */
  130.     printf( "\tz/Z = zoom one level / maximum zoom\n" );
  131.     printf( "\ts/S = shrink one level / minimum shrink\n" );
  132.     printf( "\tl/L = move left by one / move left by ten\n" );
  133.     printf( "\tr/R = move right by one / move right by ten\n" );
  134.     printf( "\tu/U = move up by one / move up by ten\n" );
  135.     printf( "\td/D = move down by one / move down by ten\n" );
  136.     printf( "\tany other key exits the program\n" );
  137.     printf( "\nPress ENTER to continue, or ^C to exit " );
  138.     regs.h.ah = 0x8; /* character input without echo */
  139.     intdos( ®s, ®s ); /* call dos to get a keystroke */
  140.     mode = GetMode(); /* save mode so can restore later */
  141.     SetMode( 0x10 ); /* 640x350 16-color mode */
  142.     doedges(); /* display board edges */
  143.     doboard(); /* display the board */
  144.     for (;;) { /* process until unrecognized keystroke */
  145.         regs.h.ah = 0x8; /* character input without echo */
  146.         intdos( ®s, ®s ); /* call dos to get a keystroke */
  147.         switch (regs.h.al) { /* determine what it is */
  148.         case 'Z': /* zoom to the limit */
  149.             if (zoom == MAXZOOM)
  150.                 continue;
  151.             zoom = MAXZOOM;
  152.             break;
  153.         case 'z': /* zoom by one */
  154.             if (zoom == MAXZOOM)
  155.                 continue;
  156.             zoom++;
  157.             break;
  158.         case 'S': /* shrink to the limit */
  159.             if (zoom == 0)
  160.                 continue;
  161.             zoom = 0;
  162.             break;
  163.         case 's': /* shrink by one */
  164.             if (zoom == 0)
  165.                 continue;
  166.             zoom--;
  167.             break;
  168.         case 'L': /* left by 10 */
  169.             if (Xcol == 0)
  170.                 continue;
  171.             if (Xcol <= 10)
  172.                 Xcol = 0;
  173.             else
  174.                 Xcol -= 10;
  175.             break;
  176.         case 'l': /* left by one */
  177.             if (Xcol == 0)
  178.                 continue;
  179.             Xcol--;
  180.             break;
  181.         case 'R': /* right by 10 */
  182.             if (Xcol == Ncols-1)
  183.                 continue;
  184.             if (Xcol >= Ncols-11)
  185.                 Xcol = Ncols-1;
  186.             else
  187.                 Xcol += 10;
  188.             break;
  189.         case 'r': /* right by one */
  190.             if (Xcol == Ncols-1)
  191.                 continue;
  192.             Xcol++;
  193.             break;
  194.         case 'U': /* up by 10 */
  195.             if (Xrow == Nrows-1)
  196.                 continue;
  197.             if (Xrow >= Nrows-11)
  198.                 Xrow = Nrows-1;
  199.             else
  200.                 Xrow += 10;
  201.             break;
  202.         case 'u': /* up by one */
  203.             if (Xrow == Nrows-1)
  204.                 continue;
  205.             Xrow++;
  206.             break;
  207.         case 'D': /* down by 10 */
  208.             if (Xrow == 0)
  209.                 continue;
  210.             if (Xrow <= 10)
  211.                 Xrow = 0;
  212.             else
  213.                 Xrow -= 10;
  214.             break;
  215.         case 'd': /* down by one */
  216.             if (Xrow == 0)
  217.                 continue;
  218.             Xrow--;
  219.             break;
  220.         default:
  221.             SetMode( mode ); /* restore original screen mode */
  222.             exit( 0 );
  223.             break;
  224.             }
  225.         SetMode( 0x10 ); /* clear screen */
  226.         doedges(); /* display board edges */
  227.         doboard(); /* display the board */
  228.         }
  229.     }
  230.  
  231. static void doedges () { /* display the board edges */
  232.     int r1, c1, r2, c2, i, z;
  233.  
  234.     z = zoomsize[zoom];
  235.     /* first, calculate their virtual screen positions */
  236.     r1 = MAXVERT+(Xrow*z); /* bottom edge */
  237.     c1 = MINHORZ-(Xcol*z); /* left edge */
  238.     r2 = MAXVERT-1-((Nrows-Xrow)*z); /* top edge */
  239.     c2 = MINHORZ+1+((Ncols-Xcol)*z); /* right edge */
  240.     if (r1 >= MINVERT && r1 <= MAXVERT) /* draw bottom edge */
  241.         for (i = c1; i <= c2; i++)
  242.             if (i >= MINHORZ && i <= MAXHORZ)
  243.                 Dot( E, r1, i );
  244.     if (c1 >= MINHORZ && c1 <= MAXHORZ) /* draw left edge */
  245.         for (i = r1; i >= r2; i--)
  246.             if (i >= MINVERT && i <= MAXVERT)
  247.                 Dot( E, i, c1 );
  248.     if (r2 >= MINVERT && r2 <= MAXVERT) /* draw top edge */
  249.         for (i = c1; i <= c2; i++)
  250.             if (i >= MINHORZ && i <= MAXHORZ)
  251.                 Dot( E, r2, i );
  252.     if (c2 >= MINHORZ && c2 <= MAXHORZ) /* draw right edge */
  253.         for (i = r1; i >= r2; i--)
  254.             if (i >= MINVERT && i <= MAXVERT)
  255.                 Dot( E, i, c2 );
  256.     }
  257.  
  258. struct x { /* group the bit templates for an object */
  259.     long t;            /* the object type    */
  260.     char t0[ZOOM0][ZOOM0];    /* tiny zoom template    */
  261.     char t1[ZOOM1][ZOOM1];    /* small zoom template    */
  262.     char t2[ZOOM2][ZOOM2];    /* medium zoom template    */
  263.     char t3[ZOOM3][ZOOM3];    /* large zoom template    */
  264.     };
  265.  
  266. extern struct x y1[]; /* hole templates */
  267.  
  268. extern int z1; /* number of hole types */
  269.  
  270. #define domap(v,c)    { for (i = 0; i < z1; i++) { \
  271.                 if (v & (y1[i].t)) { \
  272.                     if (zoom == 0) \
  273.                         plot0( rp, cp, y1[i].t0, c );\
  274.                     else if (zoom == 1) \
  275.                         plot1( rp, cp, y1[i].t1, c );\
  276.                     else if (zoom == 2) \
  277.                         plot2( rp, cp, y1[i].t2, c );\
  278.                     else if (zoom == 3) \
  279.                         plot3( rp, cp, y1[i].t3, c );\
  280.                     } \
  281.                 } }
  282.  
  283. static void doboard () { /* display the board on the screen, row by column */
  284.     int r, c, rp, cp, rpd, cpd, z, i;
  285.     int r2, c2, rp2, cp2;
  286.     char far *n;
  287.     char far *n2;
  288.     long x;
  289.  
  290.     ReInitWork();
  291.     for (z = zoomsize[zoom], GetWork( &r, &c, &n, &r2, &c2, &n2 );
  292.         r != ILLEGAL; GetWork( &r, &c, &n, &r2, &c2, &n2 )) {
  293.         rp = MAXVERT - ((r-Xrow)*z + z/2 + 1);
  294.         cp = (c-Xcol)*z + z/2 + 1;
  295.         rp2 = MAXVERT - ((r2-Xrow)*z + z/2 + 1);
  296.         cp2 = (c2-Xcol)*z + z/2 + 1;
  297.         Line( rp, cp, rp2, cp2, 1 );
  298.         }
  299.     rpd = MINVERT+z; /* top-most plottable row */
  300.     cpd = MAXHORZ-z; /* right-most plottable column */
  301.     for (r = Xrow, rp = MAXVERT-1; r < Nrows && rp >= rpd; r++, rp -= z) {
  302.         for (c = Xcol, cp = MINHORZ+1; c < Ncols && cp <= cpd;
  303.                 c++, cp += z) {
  304.             x = GetCell( r, c, TOP );
  305.             if (x & HOLE) /* only map if there is a hole */
  306.                 domap( x, H ); /* plot the hole */
  307.             }
  308.         }
  309.     }
  310.  
  311. static void plot0 ( rp, cp, obj, color ) /* plot a 3x3 template */
  312.     int rp, cp;
  313.     char obj[ZOOM0][ZOOM0];
  314.     int color;
  315.     {
  316.     int r, c;
  317.  
  318.     for (r = 0; r < ZOOM0; r++)
  319.         for (c = 0; c < ZOOM0; c++)
  320.             if (obj[r][c])
  321.                 Dot( color, rp-r, cp+c );
  322.     }
  323.  
  324. static void plot1 ( rp, cp, obj, color ) /* plot a 6x6 template */
  325.     int rp, cp;
  326.     char obj[ZOOM1][ZOOM1];
  327.     int color;
  328.     {
  329.     int r, c;
  330.  
  331.     for (r = 0; r < ZOOM1; r++)
  332.         for (c = 0; c < ZOOM1; c++)
  333.             if (obj[r][c])
  334.                 Dot( color, rp-r, cp+c );
  335.     }
  336.  
  337. static void plot2 ( rp, cp, obj, color ) /* plot a 10x10 template */
  338.     int rp, cp;
  339.     char obj[ZOOM2][ZOOM2];
  340.     int color;
  341.     {
  342.     int r, c;
  343.  
  344.     for (r = 0; r < ZOOM2; r++)
  345.         for (c = 0; c < ZOOM2; c++)
  346.             if (obj[r][c])
  347.                 Dot( color, rp-r, cp+c );
  348.     }
  349.  
  350. static void plot3 ( rp, cp, obj, color ) /* plot an 15x15 template */
  351.     int rp, cp;
  352.     char obj[ZOOM3][ZOOM3];
  353.     int color;
  354.     {
  355.     int r, c;
  356.  
  357.     for (r = 0; r < ZOOM3; r++)
  358.         for (c = 0; c < ZOOM3; c++)
  359.             if (obj[r][c])
  360.                 Dot( color, rp-r, cp+c );
  361.     }
  362.  
  363. int GetApxDist( int, int, int, int );
  364.  
  365. int GetApxDist ( r1, c1, r2, c2 ) /* calculate approximate distance - stub */
  366.     int r1, c1, r2, c2;
  367.     {
  368.     return( 0 );
  369.     r1;  c1;  r2;  c2;
  370.     }
  371.